package com.javapandeng.controller;

import com.alibaba.fastjson.JSONObject;
import com.javapandeng.base.BaseController;
import com.javapandeng.po.Message;
import com.javapandeng.service.MessageService;
import com.javapandeng.utils.Consts;
import com.javapandeng.utils.PageBean;
import com.javapandeng.utils.Pager;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 留言管理
 */
@Controller
@RequestMapping("/message")
public class MessageController extends BaseController {

    @Autowired
    private MessageService messageService;

    /**
     * 留言列表
     */
    @RequestMapping("/findBySql")
    public String findBySql(Message message, Model model){
        String sql = "select * from message where 1=1 ";
        if(!isEmpty(message.getName())){
            sql += " and name like '%"+message.getName()+"%'";
        }
        sql += " order by id desc";
        Pager<Message> pagers = messageService.findBySqlRerturnEntity(sql);
        model.addAttribute("pagers",pagers);
        model.addAttribute("obj",message);
        return "message/message";
    }

    /**
     * 删除留言
     */
    @RequestMapping("/delete")
    public String delete(Integer id){
        messageService.deleteById(id);
        return "redirect:/message/findBySql";
    }
    /**
     * 回复留言
     */


    /**
     * 发表留言进入
     */
    @RequestMapping("/add")
    public String add(){
        return "message/add";
    }

    /**
     * 发表留言
     */
    @RequestMapping("/exAdd")
    @ResponseBody
    public String exAdd(Message message, HttpServletRequest request){

        Object attribute = request.getSession().getAttribute(Consts.USERID);
        if(attribute==null){
            message.setUserId(null);
        }else {
            Integer userId = Integer.valueOf(attribute.toString());
            message.setUserId(userId);
        }

/*        JSONObject js = new JSONObject();
        if(attribute==null){
            js.put(Consts.RES,0);
            return js.toJSONString();
        }*/


        messageService.insert(message);
        JSONObject js = new JSONObject();
        js.put("message","添加成功");
        return js.toString();
    }


    /**
     * 我的消息列表
     * @param page 当前页
     * @param pageSize 页记录数
     * @param request
     * @return
     */
    @RequestMapping("/myMessageList")
    @ResponseBody
    public Map<String, Object> myMessageList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                             @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                             HttpServletRequest request) {

        Object attribute = request.getSession().getAttribute(Consts.USERID);
         HashMap js = new HashMap();
        if(attribute==null){
            js.put(Consts.RES,0);
            return js;
        }

        Integer userId = Integer.valueOf(attribute.toString());
        Message condition = new Message();
        condition.setUserId(userId);
        List<Message> messageList = messageService.listAllByEntity(condition);
        if (!messageList.isEmpty()) {
            PageBean<Message> bean = new PageBean<>();
            bean.queryPager(page, pageSize, messageList);

            js.put(Consts.RES,1);
            js.put("message","查询成功");
            js.put("data", bean.getList());
            js.put("pagesUtils", bean);
            return js;
        }

        PageBean<Message> bean = new PageBean<>();
        bean.queryPager(page, pageSize, messageList);

        js.put(Consts.RES,1);
        js.put("message","查询成功");
        js.put("data", bean.getList());
        js.put("pagesUtils", bean);
        return js;
    }


    /**
     * 回复内容
     * @param message
     * @param request
     * @return
     */
    @RequestMapping("/answer")
    public String answer(Message message, HttpServletRequest request){

        message.setAnswerTime(new Date());
        messageService.updateById(message);
        return "redirect:/message/findBySql";
    }

}
